package com.example.onion.hxchat.util;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author:onion
 * @data: 2020/6/16
 */
public class HttpUtils {
    private static HttpUtils httpUtils;
    private OkHttpClient okHttpClient;
    private HttpUtils() {
        okHttpClient = new OkHttpClient();

    }

    public static HttpUtils getInstance() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils();
                }
            }
        }

        return httpUtils;
    }

    class StringCallBack implements Callback {
        private HttpCallBack httpCallBack;
        private Request request;

        public StringCallBack(Request request, HttpCallBack httpCallBack) {
            this.request = request;
            this.httpCallBack = httpCallBack;
        }

        @Override
        public void onFailure(Call call, IOException e) {
            final IOException fe = e;
            if (httpCallBack != null) {
                httpCallBack.onError(request, fe);
            }
        }


        @Override
        public void onResponse(Call call, Response response) throws IOException {
            final String result = response.body().string();
            if (httpCallBack != null) {
                httpCallBack.onSuccess(request, result);
            }
        }

    }
    /**
     * get请求（OKhttp3）
     * @param url 请求地址
     * @param httpCallBack 回调接口
     */
    public void asyncGet(String url, HttpCallBack httpCallBack) {
        Request request = new Request.Builder().url(url).build();
        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));
    }

    /**
     * post请求（OKhttp3）
     * @param url 请求地址
     * @param requestBody 请求体
     * @param httpCallBack 回调接口
     */
    public void asyncPost(String url, RequestBody requestBody, HttpCallBack httpCallBack) {
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept","application/json")
                .build();
        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));
    }

    public void asyncDelete(String url,RequestBody requestBody,String token,HttpCallBack httpCallBack){
        Request request = new Request.Builder()
                .url(url)
                .delete(requestBody)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept","application/json")
                .addHeader("Authorization","Bearer "+token)
                .build();
        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));

    }


    public interface HttpCallBack {
        void onError(Request request, IOException e);

        void onSuccess(Request request, String result);
    }

    /**
     * post请求带环信token，专用请求环信token的（OKhttp3）
     *
     * @param url          请求地址
     * @param requestBody  请求体
     * @param token 环信token
     * @param httpCallBack 回调接口
     */
    public void asyncPostHxToken(String url, RequestBody requestBody, String token, HttpCallBack httpCallBack) {
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization","Bearer "+token)
                .build();
        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));
    }
}
